// Sound.cs
//

using System;
using System.Collections.Generic;
using System.Html;

using Moonbeam.Core;

namespace Moonbeam.Media
{
    public class Sound
    {
        AudioElement element;
        public bool IsActive;
        float targetVolume;
        bool fadeIn;

        public Sound(string path)
        {
            element = (AudioElement)Document.CreateElement("audio");
            element.Src = path;            
            
        }

        public void Play()
        {
            IsActive = true;
            element.Play();
        }

        public void Stop()
        {
            element.Pause();
            IsActive = false;
        }

        public void FadeIn()
        {
            fadeIn = true;
            element.Volume = 0;
            Play();
        }

        public void Update(GameTime gameTime)
        {
            if (fadeIn)
            {
                if (element.Volume < targetVolume)
                {
                    element.Volume++;
                }
            }
            else
            {
                element.Volume = targetVolume;
            }

            IsActive = !element.Ended;
        }

        public void SetVolume(float _volume)
        {
            if (_volume >1.0f)
            {
                _volume = 1.0f;
            }

            if (_volume < 0.0f)
            {
                _volume = 0.0f;
            }

            targetVolume = _volume;
        }
    }
}
